<template>
  <div class="container">
    <!-- 卡片组件 -->
    <el-card class="box-card">
      <!-- 顶部搜索 -->
      <div class="el-card_box">
        <!-- 搜索区 -->
        <div class="search">
          <div>
            <span>关键字</span>
            <el-input v-model="keyword" class="ipt"></el-input>
          </div>
          <div class="btn">
            <el-button size="mini" @click="removeFn">清除</el-button>
            <el-button type="primary" size="mini" @click="searchFn"
              >搜索</el-button
            >
          </div>
        </div>
      </div>
      <!-- /顶部搜索 -->

      <!-- 消息提示 -->
      <el-alert
        :title="`数据一共${counts}条`"
        type="info"
        show-icon
        :closable="false"
        class="tishi"
      >
      </el-alert>
      <!-- /消息提示 -->

      <!-- 表格区域 -->
      <el-table :data="randomList">
        <el-table-column prop="id" label="编号" width="190"> </el-table-column>
        <el-table-column prop="name" label="题型">
          <template v-slot="scoped">
            <span v-if="scoped.row.questionType === '1'">单选</span>
            <span v-else-if="scoped.row.questionType === '2'">多选</span>
            <span v-else>简单</span>
          </template>
        </el-table-column>
        <el-table-column label="题目编号" width="200">
          <template v-slot="scoped">
            <div class="num">
              <a
                v-for="(items, ind) in scoped.row.questionIDs"
                :key="ind"
                @click="preview(scoped.row, items)"
                >{{ items.number }}<br
              /></a>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="addTime" label="录入时间"> </el-table-column>
        <el-table-column prop="totalSeconds" label="答题时间(s)">
        </el-table-column>
        <el-table-column prop="accuracyRate" label="正确率(%)">
        </el-table-column>
        <el-table-column prop="userName" label="录入人"> </el-table-column>
        <el-table-column prop="username" label="操作">
          <template v-slot="scoped">
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="deleteFn(scoped.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- /表格表格 -->

      <!-- 分页功能 -->
      <el-pagination
        layout="prev,pager,next,sizes,jumper"
        :total="counts"
        background
        :current-page="page"
        :page-sizes="[20, 40, 60, 80]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-size="pagesize"
      >
      </el-pagination>
      <!-- /分页功能 -->
    </el-card>
    <!-- 弹窗 -->
    <questions-preview
      :dialogVisible="dialogVisible"
      @close="dialogVisible = false"
      :questionItem="questionItem"
    ></questions-preview>
  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions'
// 预览组件
import questionsPreview from '../components/questions-preview'
export default {
  data() {
    return {
      // 搜索关键字
      keyword: '',
      // 页码
      page: 1,
      // 页数
      pagesize: 20,
      // 总数据
      counts: 0,
      // 表格数据
      randomList: [],
      DialogItem: {},
      questionItem: {},
      isDialogShow: false,
      // 试题预览
      dialogVisible: false,
      checkDialogVisible: false,
      // 难度
      questionType: {
        1: '单选',
        2: '多选',
        3: '简答'
      }
    }
  },
  components: {
    questionsPreview
  },
  created() {
    this.loadRandoms(this.page, this.pagesize)
  },
  methods: {
    async loadRandoms(page, pagesise, keyword) {
      try {
        const { data } = await randoms({
          page,
          pagesise,
          keyword
        })
        console.log(data)
        this.counts = data.counts
        this.randomList = data.items
      } catch (err) {
        console.log('获取数据失败')
      }
    },
    // 删除功能
    deleteFn(item) {
      this.$confirm('此操作将永久删除该组题, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await removeRandoms({
            id: item.id
          })
          this.loadRandoms()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 更新页数
    handleSizeChange(val) {
      this.pagesize = val
      this.page = 1
      this.loadRandoms(this.page)
    },
    // 更新页码
    handleCurrentChange(page) {
      this.page = page
      this.loadRandoms(this.page)
    },
    removeFn() {
      this.keyword = ''
      this.loadRandoms()
    },
    // 搜索
    searchFn() {
      this.page = 1
      this.pagesize = 20
      // console.log(this.page, this.pagesize, this.keyword)
      this.loadRandoms(this.page, this.pagesize, this.keyword)
      this.$message.success('搜索成功')
    },
    async preview(row, item) {
      // 显示对话框
      console.log(row)
      console.log(item)
      const { data } = await detail(item)
      console.log(data)
      data.questionType = row.questionType
      data.subject = row.subject
      this.dialogVisible = true
      // 传入试题
      this.questionItem = data
    }
  }
}
</script>

<style scoped lang="less">
.container {
  padding: 20px;
  .search {
    display: flex;
    justify-content: space-between;
    span {
      font-size: 12px;
      color: #000;
    }
    .ipt {
      margin-left: 15px;
      width: 303px;
      height: 37px;
    }
  }
  // 消息提示框
  .tishi {
    margin: 18px 0;
  }
  /deep/.num {
    color: #0099ff;
  }
  .el-pagination {
    margin: 20px;
    float: right;
  }
}
</style>
